   -- variabili personalizzabili
dropbag = UO.BackpackID
hotkey = "F5"
lootgold = true
lootgems = false
lootpeerless = true
lootreagent = false


debug = true
hidelooted = false

minvalue = 0
dofile(getinstalldir().."scripts/item_evaluator.lua") 


--inizializzazione costanti e tavole
corpsetype = 8198
oldcorpses = {}
errmsg = {
  ["Something is standing in your way"] = true,
  ["That is out of sight."] = true,
  ["You must wait to perform another action."] = true,
  ["You can not pick that up."] = true,
  ["Target cannot be seen."] = true,
  [UO.CharName..": I can't reach that."] = true,
  ["You did not earn the right to loot this creature!"] = true,
  }  
  
-- Liste loot
gems = {3855,3856,3857,3859,3861,3862,3864,3865,3873,3877,3878,3885}
gold = {3821}
peerlesskeys = {3829,3975,4328,6880,7391,7406,7408,7583,8193,8194,8765,8766,8770
,8773,8775,8776,22334}
reagents = {3960,3962,3963,3965,3966,3968,3969,3972,3973,3974,3976,3978,3980
,3981,3982,3983,12688,12689,16503}

-- INIZIO SCRIPT 

function buildlootlist ()
lootlist = {}
if lootgold then
   for k,v in pairs(gold) do
      lootlist[v] = "lootable" 
      end
end
if lootgems then
    for k,v in pairs(gems) do
      lootlist[v] = "lootable"
      end
    end
if lootreagent then
   for k,v in pairs(reagents) do
      lootlist[v] = "lootable" 
      end
   end
if lootpeerless then
   for k,v in pairs(peerlesskeys) do
      lootlist[v] = "lootable" 
      end
   end
   
end

--presa da choosy non mi piace, DA RIFARE
--[[Features: debug
    Parameter: range = 'number'
    Return: true at first matching item
            false if nothing found
]]--

function FindCorpse(range)
  local i, c, dx, dy
  for i = 0,UO.ScanItems(true)-1 do
    local id,type,kind,contid,x,y,z,stack,rep,col = UO.GetItem(i)
    if type == 8198 then
      local skip = false
      for c = 1,#oldcorpses do
        if id == oldcorpses[c] then
          skip = true
        end
      end
      if not skip then
        dx = math.abs(x-UO.CharPosX)
        dy = math.abs(y-UO.CharPosY)
        if dy < range and dx < range then
          corpse = id
          return true
        end
      end
    end
  end 
  return false  
end


--[[Parameter needed: global variable corpse = 'ID'
    Returns: false if timed out or error occurs   
             true and sets variables if success
             public variables: ccX
                               ccY
                               corpseCont
]]--

function OpenCorpse()
  UO.LObjectID = corpse
  local timeout = getticks() + 2000
  local tempCont = UO.ContID
  UO.Macro(17, 0, "")
  wait (100)
  repeat 
    if checkMsg(timeout - getticks()) then
      print("OpenCorpse function error: not possible to complete")
      return false
    end
    if corpsetype == UO.ContType and corpse == UO.ContID then
     corpseCont = UO.ContID
     ccX = UO.ContPosX + 75
     ccY = UO.ContPosY + 75
     return true
    end
    wait(5)
  until timeout < getticks()
return false
end

--[[Parameter: timeout = 'number'
    return: true if message is found
            false if nothing
]]--

function checkMsg(timeout)
  local ref = 0
  local i, jstr, col, cnt
  ref,cnt = UO.ScanJournal(ref)
  repeat
    wait(5)
    ref,cnt = UO.ScanJournal(ref)
  until cnt > 0 or timeout < getticks()
  if cnt < 1 then
    return false
  end
  for i = cnt-1,0,-1 do
    jstr,col = UO.GetJournal(i)
    if errmsg[jstr] then
      print(jstr)
      return true
    end
    wait (5)
  end
return false
end

function CloseCorpse()
  while UO.ContID == corpseCont do
    UO.Click(ccX,ccY,false,true,true,false)
    wait (20)
  end
  UO.HideItem(corpse)
end

function dropitem(it,amt,bag)
  UO.Drag(it,amt)
  wait(100)
  repeat
  UO.DropC(dropbag)
  until not checkMsg(2000) 
  wait(210)
    --print("drag item error")
    --return false
  --end
  return true
end

function CheckLoot()
  lootTable = {}
  itemAmt = {}
  local i, j
  local itemvalue = 0 
  local cnt = UO.ScanItems(true)-1
  for i=cnt, 0 , -1 do
    id,type,kind,contid,x,y,z,stack,rep,col = UO.GetItem(i)
    if itemtypesindex[type] ~= nil then
      print("item riconosciuto")
       if itemtypesindex[type][1] == "j" and  minvalue > 0 then
          itemvalue = evaluate(id)
           name = UO.Property(id)
           print ( name .. " Valued: " .. itemvalue .. " type: " .. type )
           if itemvalue > minvalue then
              table.insert(lootTable,id)
              table.insert(itemAmt,1)
           end
       end
    end
    if contid == corpseCont then
       if lootlist[type] == "lootable"  then
          table.insert(lootTable,id)
          table.insert(itemAmt,stack)
          name, info = UO.Property(id)
       end
    end
  end
  if #lootTable == 0 or #lootTable == nil then
    if hidelooted then CloseCorpse () end
    return false
  end
  return true
end


function LootCorpse()
  local i,timeout
  timeout = getticks() + 20000 
  CheckLoot ()
  repeat  
  for i = 1, #lootTable do
    if not dropitem(lootTable[i],itemAmt[i],lootbag) then
      print ("error looting?")
      return false
    end
  end
  until not CheckLoot () 
  return true
end


timestamp = function(ms)
--UO.SysMessage ("Operazione completata in " .. getticks() - ms .. " millisecondi" , 51)
print("Operazione completata in " .. getticks() - ms .. " millisecondi") 
end

function main()
if UO.Weight >= maxweight - 40 then UO.SysMessage ( "Nearly overweight! Please unload before continuing looting." , 233) return false end
   if FindCorpse (3) then
      timer = getticks()
      if OpenCorpse () then
        if LootCorpse () then
          UO.SysMessage ( "looted in " .. getticks() - timer .. "ms" , 233)  
          if hidelooted then CloseCorpse () end 
          table.insert(oldcorpses,corpse)   
        end
      else
          UO.SysMessage ( "looted in " .. getticks() - timer .. "ms" , 233)  
      end
   end
end

isHuman = function ()
return (UO.CharType == 400 or UO.CharType == 401 or UO.CharType == 402 or UO.CharType == 403 )
end

--start executing
if isHuman then maxweight = UO.MaxWeight + 60 else maxweight = UO.MaxWeight end 
buildlootlist()
repeat 
wait (5)
if getkey(hotkey) then
  main()
  wait(600)
  end
until false == true